﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Web.Services;
using PPI.Data;
using PPI.Entities;
using PPI.Entities.Distribution;
using PPI.Entities.Enumerator;
using PPI.Entities.Inspections;
using PPI.Entities.Lookup;
using PPI.Entities.Settings.Mobile;
using PPI.ServiceLogic;
using PPI.ServiceLogic.Deploy;
using PPI.ServiceLogic.Inspections;
using PPI.ServiceLogic.Mobile;
using PPI.ServiceLogic.Polygon;
using PPI.ServiceLogic.Remedy;
using PPI.ServiceLogic.Utilty;

namespace PPIWebService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://www.dev-poleposition.com/deploy/Webservice2.1/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        #region Deploy

        [WebMethod]
        public string Deploy_StartDeployGPS(int utilityId,string utilityName, int vendorId, string vendorName, int projectId, string projectName, int contactId, string contactName, int handheldId, string handheldName, string connectionKey, string mobileVersion)
        {
            string sourceFile = Server.MapPath("") + MobileConstants.K_SERVICEEMPTYGPSDATABASE;
            
            EMobileSettingsBase _Settings =
                new EMobileSettingsGPS(new EHandheld(handheldId,handheldName), new EVendor(vendorId,vendorName,""),
                                       new EVendor_Contact(contactId,contactName), new EVendor_Project(projectId,projectName),
                                       new EUtility(utilityId, utilityName), mobileVersion);

            return SetupDeploy(_Settings, connectionKey, sourceFile);
        }

        [WebMethod]
        public string Deploy_StartDeployInspection(int handheldId, string handheldName,int inspectorId, string inspectorName, int companyId, string companyName,string connectionKey, string mobileVersion, int vendorId, string vendorName, int projectId, string projectName, int contactId, string contactName)
        {
            string sourceFile = Server.MapPath("") + MobileConstants.K_SERVICEEMPTYINSPECTIONSDATABASE;
            //string sourceFile = @"C:\emptyInspections.sdf";
            EMobileSettingsBase _Settings =
                new EMobileSettingsInspection(new ECompany(companyId, companyName), new EInspector(inspectorId, inspectorName), new EHandheld(handheldId, handheldName), mobileVersion, new EVendor(vendorId, vendorName, ""),
                                       new EVendor_Contact(contactId, contactName), new EVendor_Project(projectId, projectName));

            return SetupDeploy(_Settings, connectionKey, sourceFile);
        }

        [WebMethod]
        public string Deploy_StartDeployProject(int handheldId, string handheldName,string connectionKey, string mobileVersion)
        {
            string sourceFile = Server.MapPath("") + MobileConstants.K_SERVICEEMPTYPROJECTDATABASE;
            //string sourceFile = @"C:\emptyProject.sdf";
            EMobileSettingsBase _Settings = new EMobileSettingsProject(new EHandheld(handheldId, handheldName), mobileVersion);
            
            return SetupDeploy(_Settings, connectionKey, sourceFile);
        }

        [WebMethod]
        //public string Deploy_StartDeployDistribution(int handheldId, string handheldName, string connectionKey, string mobileVersion)
        public string Deploy_StartDeployDistribution(string connectionKey, int distributionAppMode)
        {
            string sourceFile = Server.MapPath(string.Empty) + MobileConstants.K_SERVICEEMPTYDISTRIBUTIONDATABASE;
            //string sourceFile = @"C:\emptyDistribution.sdf";

            EMobileSettingsBase _Settings =
                //new EMobileSettingDistribution(new EHandheld(handheldId, handheldName), mobileVersion);
                new EMobileSettingDistribution(distributionAppMode);

            return SetupDeploy(_Settings, connectionKey, sourceFile);
        }

        [WebMethod]
        public string Deploy_StartDeployDistribution_Vendor(string connectionKey, int distributionAppMode, int vendorId, string vendorName, int vendorContactId, string vendorContactName, int vendorProjectId, int utilityId, string utilityName)
        {
            string sourceFile = Server.MapPath(string.Empty) + MobileConstants.K_SERVICEEMPTYDISTRIBUTIONDATABASE;
            //string sourceFile = @"C:\emptyDistribution.sdf";
            EVendor_Contact vendorContact = new EVendor_Contact(vendorContactId, vendorContactName);
            EVendor vendor = new EVendor(vendorId, vendorName, string.Empty);
            EVendor_Project vendorProject = new EVendor_Project(vendorProjectId);
            EUtility utility = new EUtility(utilityId, utilityName);
            vendorContact.Vendor = vendor;

            EMobileSettingsBase _Settings =
                //new EMobileSettingDistribution(new EHandheld(handheldId, handheldName), mobileVersion);
                //new EMobileSettingDistribution(distributionAppMode);
                new EMobileSettingDistribution(distributionAppMode, vendorContact, vendor, vendorProject, utility);

            return SetupDeploy(_Settings, connectionKey, sourceFile);
        }

        private string SetupDeploy(EMobileSettingsBase settings, string connectionKey, string sourceFile)
        {
            //Create hashFolder
            string _NewFolderName = Server.MapPath("") + "\\" + DateTime.Now.Ticks.ToString();
            DirectoryInfo _ServerFolder = new DirectoryInfo(Server.MapPath(""));
            _ServerFolder = _ServerFolder.CreateSubdirectory("MobileDatabases");
            
            switch(settings.DatabaseType)
            {
                case DatabaseTypes.GPS:
                    _ServerFolder = _ServerFolder.CreateSubdirectory("GPS");
                    break;
                case DatabaseTypes.Inspection:
                    _ServerFolder = _ServerFolder.CreateSubdirectory("Inspections");
                    break;
                case DatabaseTypes.Project:
                    _ServerFolder = _ServerFolder.CreateSubdirectory("Project");
                    break;
                case DatabaseTypes.DistributionInspection:
                    _ServerFolder = _ServerFolder.CreateSubdirectory("DistributionInspection");
                    break;
                case DatabaseTypes.DistributionGPS:
                    _ServerFolder = _ServerFolder.CreateSubdirectory("DistributionGPS");
                    break;
            }

            try
            {
                DeleteMobileFolderLessToday(_ServerFolder);
            }
            catch(Exception ex)
            {
                
            }
            
            string destFile = _ServerFolder.CreateSubdirectory(DateTime.Now.Ticks.ToString()).FullName + "\\Temp " + settings.DatabaseType.ToString() + ".sdf";

            DeployBase _Deploy = new DeployBase(settings, sourceFile, destFile, connectionKey);
            try
            {
                _Deploy.ConfigMobileDatabase();
                _Deploy.FillMobileTables();
            }
            catch(Exception ex)
            {
                return ex.Message;
                //throw ex;
            }

            return _Deploy.NewMobileDatabase;
}

        private void DeleteMobileFolderLessToday(DirectoryInfo _ServerFolder)
        {
            //Clean all server folders
            foreach (DirectoryInfo f in _ServerFolder.GetDirectories())
            {
                DateTime tempDate = DateTime.MinValue.AddMilliseconds(long.Parse(f.Name)/10000);
                if(tempDate < DateTime.Now)
                    f.Delete(true);
            }
        }

        #endregion

        #region Report

        [WebMethod]
        public DataSet Report_GetReportPolesByAge(Int32 projectId, string connectionKey)
        {
            return LReport.GetReportPolesByAge(projectId, connectionKey);
        }

        [WebMethod]

        public DataSet Report_GetReportVisitsByTop(Int32 projectId, string connectionKey)
        {
            return LReport.GetReportVisitsByTop(projectId, connectionKey);
        }

        [WebMethod]
        public DataSet Report_GetReportVisitByWeekContact(string connectionKey)
        {
            return LReport.GetReportVisitsByWeekAndContact(connectionKey);
        }

        [WebMethod]
        public DataSet Report_GetReportPoleByWeekContact(string connectionKey)
        {
            return LReport.GetReportPolesByWeekAndContact(connectionKey);
        }

        [WebMethod]
        public DataSet Report_GetReportInspectionsByWeekContact(string connectionKey)
        {
            return LReport.GetReportInspectionByWeekAndContact(connectionKey);
        }

        [WebMethod]
        public DataSet Report_GetReportInspectionsTypeByWeekInspector(string connectionKey)
        {
            return LReport.GetReportInspectionTypeByWeekAndInspector(connectionKey);
        }

        [WebMethod]
        public DataSet Report_GetReportInspectionsTypeByWeekInspectorByProject(int projectId, string connectionKey)
        {
            return LReport.GetReportInspectionTypeByWeekAndInspector(projectId, connectionKey);
        }

        [WebMethod]
        public DataSet Report_GetReportDistributionInspectionsTypeByWeekInspector(string connectionKey)
        {
            return LReport.GetReportDistributionInspectionTypeByWeekAndInspector(connectionKey);
        }

        [WebMethod]
        public DataSet Report_GetReportProjectSummary(string connectionKey)
        {
            return LReport.GetReportProjectSummary(connectionKey);
        }

        [WebMethod]
        public DataSet Report_GetReportProjectSummaryByProject(int idProject, string connectionKey)
        {
            return LReport.GetReportProjectSummary(idProject, connectionKey);
        }

        [WebMethod]
        public DataSet Report_GetReportDistributionInspectionProjectSummary(string connectionKey)
        {
            return LReport.GetDistributionInspectionReportProjectSummary(connectionKey);
        }
        

        [WebMethod]
        public DataSet Report_GetReportLineProblems(string connectionKey)
        {
            return LReport.GetReportLineProblems(connectionKey);
        }
        [WebMethod]
        public DataSet Report_GetReportIssueByResult(string connectionKey)
        {
            return LReport.GetReportIssueByResult(connectionKey);
        }

        [WebMethod]
        public DataSet Report_GetAssetDescription(Int32 assetId, string connectionKey)
        {
            return LReport.GetReportAssetDescription(assetId, connectionKey);
        }

        #endregion

        #region Distribution
        
        [WebMethod]
        public Int32 DistributionPole_Save(Int32 serverId, Int32 mapFk, Int32 sectionFk, Int32 specieFk, Int32 classFk, Int32 lengthFk, Int32 treatmentFk, Double latitude, Double longitude, string inspBy, string number, DateTime acquisitionDate, Int32 year, DateTime lVisit, Int32 dType, string d804, int vendorId, int projectId, int inspectorId, int guardsQuantity, bool poleTag, bool groundWireRepair, bool billable, string circuit, int idKv, int idConductor, int idACSR, string connectionKey)
        {
            EDistributionPole pole = new EDistributionPole();
            
            pole.ServerID = serverId;
            pole.Map.Id = mapFk;
            pole.Section.Id = sectionFk;
            pole.Specie.Id = specieFk;
            pole.Class.Id = classFk;
            pole.Length.Id = lengthFk;
            pole.Treatment.Id = treatmentFk;
            pole.Latitude = latitude;
            pole.Longitude = longitude;
            pole.InspBy = inspBy;
            pole.Number = number;
            pole.AcquisitionDate = acquisitionDate;
            pole.Year = year;
            pole.LVisit = lVisit;
            pole.DistributionType = (DistributionType)dType;
            pole.D804 = d804;
            pole.Inspector.Id = inspectorId;
            pole.Vendor = new EVendor(vendorId);
            pole.Project = new EVendor_Project(projectId);
            pole.GuardsQuantity = guardsQuantity;
            pole.PoleTag = poleTag;
            pole.GroundWireRepair = groundWireRepair;
            pole.Billable = billable;
            pole.Circuit = circuit;
            pole.Kv.Id = idKv;
            pole.ConductorType.Id = idConductor;
            pole.ACSR.Id = idACSR;

            return PPI.ServiceLogic.Distribution.LDistributionPole.Insert(pole, connectionKey);
        }

        [WebMethod]
        public Int32 DistributionPole_Update(Int32 serverId, Int32 mapFk, Int32 sectionFk, Int32 specieFk, Int32 classFk, Int32 lengthFk, Int32 treatmentFk, Double latitude, Double longitude, string inspBy, string number, DateTime acquisitionDate, Int32 year, DateTime lVisit, Int32 dType, string d804, int vendorId, int projectId, int inspectorId, int guardsQuantity, bool poleTag, bool groundWireRepair, bool billable, string circuit, int idKv, string connectionKey)
        {
            EDistributionPole pole = new EDistributionPole();

            pole.ServerID = serverId;
            pole.Map.Id = mapFk;
            pole.Section.Id = sectionFk;
            pole.Specie.Id = specieFk;
            pole.Class.Id = classFk;
            pole.Length.Id = lengthFk;
            pole.Treatment.Id = treatmentFk;
            pole.Latitude = latitude;
            pole.Longitude = longitude;
            pole.InspBy = inspBy;
            pole.Number = number;
            pole.AcquisitionDate = acquisitionDate;
            pole.Year = year;
            pole.LVisit = lVisit;
            pole.DistributionType = (DistributionType)dType;
            pole.D804 = d804;
            pole.Inspector.Id = inspectorId;
            pole.Vendor = new EVendor(vendorId);
            pole.Project = new EVendor_Project(projectId);
            pole.GuardsQuantity = guardsQuantity;
            pole.PoleTag = poleTag;
            pole.GroundWireRepair = groundWireRepair;
            pole.Billable = billable;
            pole.Circuit = circuit;
            pole.Kv.Id = idKv;

            return PPI.ServiceLogic.Distribution.LDistributionPole.Update(pole, connectionKey);
        }

        [WebMethod]
        public void DistributionPole_Delete(Int32 poleId, string connectionKey)
        {
            PPI.ServiceLogic.Distribution.LDistributionPole.Delete(poleId, connectionKey);
        }

        [WebMethod]
        public void DistributionPole_DeleteImages(int[] poleImagesId, string connectionKey)
        {
            PPI.ServiceLogic.Distribution.LDistributionPole.DeleteImages(poleImagesId, connectionKey);
        }


        [WebMethod]
        public Int32 DistributionInspectionResult_Save(Int32 poleId, Int32 serverId, Int32 inspectionTypeFk, Int32 resultTypeFk, bool isElectrical, string comments, string connectionKey)
        {
            EDistributionInspectionResult inspectionResult = new EDistributionInspectionResult();

            inspectionResult.ServerId = serverId;
            inspectionResult.InspectionType.Id = inspectionTypeFk;
            inspectionResult.Resultype.Id = resultTypeFk;
            inspectionResult.Comments = comments;
            inspectionResult.IsElectrical = isElectrical;

            return PPI.ServiceLogic.Distribution.LDistributionPole.Insert(inspectionResult, poleId, connectionKey);
        }

        [WebMethod]
        public void DistributionInspectionResult_Update(Int32 poleId, Int32 serverId, Int32 inspectionTypeFk, Int32 resultTypeFk, bool isElectrical, string comments, string connectionKey)
        {
            EDistributionInspectionResult inspectionResult = new EDistributionInspectionResult();

            inspectionResult.Id = serverId;
            inspectionResult.InspectionType.Id = inspectionTypeFk;
            inspectionResult.Resultype.Id = resultTypeFk;
            inspectionResult.Comments = comments;
            inspectionResult.IsElectrical = isElectrical;

            PPI.ServiceLogic.Distribution.LDistributionPole.Update(inspectionResult, poleId, connectionKey);
        }

        [WebMethod]
        public List<EXMissionStructures_PoleTopType> DistributionTop_Save(Int32 poleId, List<EXMissionStructures_PoleTopType> tops, string connectionKey)
        {
            foreach (EXMissionStructures_PoleTopType top in tops)
                top.ServerId = PPI.ServiceLogic.Distribution.LTop.Insert(top, poleId, connectionKey);

            return tops;
        }

        [WebMethod]
        public List<EXMissionStructures_PoleTopType> DistributionTop_Update(Int32 poleId, List<EXMissionStructures_PoleTopType> tops, string connectionKey)
        {
            PPI.ServiceLogic.Distribution.LTop.Update(tops, poleId, connectionKey);

            return tops;
        }

        [WebMethod]
        public List<EPhase> DistributionPhase_Save(Int32 poleId, List<EPhase> phases, string connectionKey)
        {
            foreach (EPhase phase in phases)
                phase.ServerId = PPI.ServiceLogic.Distribution.LPhase.Insert(phase, poleId, connectionKey);

            return phases;
        }

        [WebMethod]
        public List<EPhase> DistributionPhase_Update(Int32 poleId, List<EPhase> phases, string connectionKey)
        {
            PPI.ServiceLogic.Distribution.LPhase.Update(phases, poleId, connectionKey);

            return phases;
        }

        [WebMethod]
        public List<PPI.Entities.Distribution.ENote> DistributionNote_Save(Int32 poleId, List<PPI.Entities.Distribution.ENote> notes, string connectionKey)
        {
            foreach (PPI.Entities.Distribution.ENote note in notes)
                note.ServerId = PPI.ServiceLogic.Distribution.LDistributionPole.Insert(note, poleId, connectionKey);

            return notes;
        }

        [WebMethod]
        public List<PPI.Entities.Distribution.ENote> DistributionNote_Update(Int32 poleId, List<PPI.Entities.Distribution.ENote> notes, string connectionKey)
        {
            PPI.ServiceLogic.Distribution.LDistributionPole.Update(notes, poleId, connectionKey);

            return notes;
        }

        [WebMethod]
        public List<EMeter> DistributionMeter_Save(Int32 poleId, List<PPI.Entities.Distribution.EMeter> meters, string connectionKey)
        {
            foreach (EMeter meter in meters)
                meter.ServerId = PPI.ServiceLogic.Distribution.LDistributionPole.Insert(meter, poleId, connectionKey);

            return meters;
        }

        [WebMethod]
        public List<ETransformer> DistributionTransformer_Save(Int32 poleId, List<ETransformer> transformers, string connectionKey)
        {
            foreach (ETransformer transformer in transformers)
                transformer.ServerId = PPI.ServiceLogic.Distribution.LDistributionPole.Insert(transformer, poleId, connectionKey);

            return transformers;
        }

        [WebMethod]
        public List<ESectionalizer> DistributionSectionalizer_Save(Int32 poleId, List<ESectionalizer> sectionalizers, string connectionKey)
        {
            foreach (ESectionalizer sectionalizer in sectionalizers)
                sectionalizer.ServerId = PPI.ServiceLogic.Distribution.LDistributionPole.Insert(sectionalizer, poleId, connectionKey);

            return sectionalizers;
        }

        [WebMethod]
        public List<ERecloser> DistributionRecloser_Save(Int32 poleId, List<ERecloser> reclosers, string connectionKey)
        {
            foreach (ERecloser recloser in reclosers)
                recloser.ServerId = PPI.ServiceLogic.Distribution.LDistributionPole.Insert(recloser, poleId, connectionKey);

            return reclosers;
        }

        [WebMethod]
        public void DistributionMeter_Delete(Int32 meterId, string connectionKey)
        {
            PPI.ServiceLogic.Distribution.LDistributionPole.DeleteMeter(meterId, connectionKey);
        }

        [WebMethod]
        public EMeter DistributionMeter_Update(Int32 poleId, EMeter meter, string connectionKey)
        {
            PPI.ServiceLogic.Distribution.LDistributionPole.Update(meter, poleId, connectionKey);

            return meter;
        }

        [WebMethod]
        public List<PPI.Entities.Distribution.ENote> DistributionNotesMeter_Save(Int32 meterId, List<PPI.Entities.Distribution.ENote> notes, string connectionKey)
        {
            foreach (PPI.Entities.Distribution.ENote note in notes)
                note.ServerId = PPI.ServiceLogic.Distribution.LDistributionPole.InsertMeterNote(note, meterId, connectionKey);

            return notes;
        }

        [WebMethod]
        public void DistributionNotesTransformers_Save(Int32 transformerId, List<PPI.Entities.Distribution.EDistributionLookUp> notes, string connectionKey)
        {
            foreach (PPI.Entities.Distribution.EDistributionLookUp note in notes)
                PPI.ServiceLogic.Distribution.LDistributionPole.InsertTransformerNote(note, transformerId, connectionKey);
        }

        [WebMethod]
        public void DistributionNotesSectionalizers_Save(Int32 sectionalizerId, List<EDistributionLookUp> notes, string connectionKey)
        {
            foreach (PPI.Entities.Distribution.EDistributionLookUp note in notes)
                PPI.ServiceLogic.Distribution.LDistributionPole.InsertSectionalizerNote(note, sectionalizerId, connectionKey);
        }

        [WebMethod]
        public void DistributionNotesReclosers_Save(Int32 recloserId, List<EDistributionLookUp> notes, string connectionKey)
        {
            foreach (PPI.Entities.Distribution.EDistributionLookUp note in notes)
                PPI.ServiceLogic.Distribution.LDistributionPole.InsertRecloserNote(note, recloserId, connectionKey);
        }

        [WebMethod]
        public Int32 DistributionPoleImage_Save(Int32 poleId, string fileName, string connectionKey)
        {
            EDistributionPoleImage image = new EDistributionPoleImage();

            image.FileName = fileName;

            return PPI.ServiceLogic.Distribution.LDistributionPole.Insert(image, poleId, connectionKey); 
        }

        [WebMethod]
        public void DistributionMeterImage_Save(Int32 meterId, string fileName, string connectionKey)
        {
            EDistributionPoleImage image = new EDistributionPoleImage();

            image.FileName = fileName;

            PPI.ServiceLogic.Distribution.LDistributionPole.MeterImageUpdate(image, meterId, connectionKey);
        }

        [WebMethod]
        public Int32 DistributionTransformerImage_Save(Int32 transformerId, string fileName, string connectionKey)
        {
            EDistributionPoleImage image = new EDistributionPoleImage();

            image.FileName = fileName;

            return PPI.ServiceLogic.Distribution.LDistributionPole.TransformerImageInsert(image, transformerId, connectionKey);
        }

        [WebMethod]
        public Int32 DistributionSectionalizerImage_Save(Int32 sectionalizerId, string fileName, string connectionKey)
        {
            var image = new EDistributionPoleImage();

            image.FileName = fileName;

            return PPI.ServiceLogic.Distribution.LDistributionPole.SectionalizerImageInsert(image, sectionalizerId, connectionKey);
        }

        [WebMethod]
        public Int32 DistributionRecloserImage_Save(Int32 recloserId, string fileName, string connectionKey)
        {
            var image = new EDistributionPoleImage();

            image.FileName = fileName;

            return PPI.ServiceLogic.Distribution.LDistributionPole.RecloserImageInsert(image, recloserId, connectionKey);
        }


        [WebMethod]
        public DataSet Distribution_GetLookUps(string connectionKey)
        {
            return PPI.ServiceLogic.Distribution.LDistributionLookup.SelectAllIdName(connectionKey);
        }

        [WebMethod]
        public DataSet DistributionMeter_GetWithoutImages(string connectionKey)
        {
            return PPI.ServiceLogic.Distribution.LDistributionPole.SelectWithoutImages(connectionKey);
        }

        [WebMethod]
        public DataSet DistributionSpecies_SelectAllIdName(string connectionKey)
        {
            return PPI.ServiceLogic.Distribution.LXMissionStructures_PoleSpecies.SelectAllIdName(connectionKey);
        }

        [WebMethod]
        public DataSet DistributionClass_SelectAllIdName(string connectionKey)
        {
            return PPI.ServiceLogic.Distribution.LXMissionStructures_PoleClass.SelectAllIdName(connectionKey);
        }

        [WebMethod]
        public DataSet DistributionTreatments_SelectAllIdName(string connectionKey)
        {
            return PPI.ServiceLogic.Distribution.LXMissionStructures_PoleTreatments.SelectAllIdName(connectionKey);
        }

        [WebMethod]
        public DataSet DistributionLenghts_SelectAllIdName(string connectionKey)
        {
            return PPI.ServiceLogic.Distribution.LLength.SelectAllIdName(connectionKey);
        }

        [WebMethod]
        public DataSet DistributionPhases_SelectAllIdName(string connectionKey)
        {
            return PPI.ServiceLogic.Distribution.LPhase.SelectAllIdName(connectionKey);
        }

        [WebMethod]
        public DataSet DistributionTops_SelectAllIdName(string connectionKey)
        {
            return PPI.ServiceLogic.Distribution.LTop.SelectAllIdName(connectionKey);
        }

        [WebMethod]
        public DataSet DistributionTops_SelectAllIdNameFromId(string connectionKey, Int32 fromId)
        {
            return PPI.ServiceLogic.Distribution.LTop.SelectAllIdName(connectionKey, fromId);
        }

        [WebMethod]
        public DataSet DistributionMaps_SelectAllIdName(string connectionKey)
        {
            return PPI.ServiceLogic.Distribution.LMap.SelectAll(connectionKey);
        }

        [WebMethod]
        public DataSet DistributionResultTypes_SelectAllIdName(string connectionKey)
        {
            return PPI.ServiceLogic.Distribution.LDistributionResultType.SelectAllIdName(connectionKey);
        }

        [WebMethod]
        public DataSet DistributionInspectionTypes_SelectAllIdName(string connectionKey)
        {
            return PPI.ServiceLogic.Distribution.LInspectionType.SelectAllIdName(connectionKey);
        }

        [WebMethod]
        public DataSet DistributionNotes_SelectAllIdName(string connectionKey)
        {
            return PPI.ServiceLogic.Distribution.LNote.SelectAllIdName(connectionKey);
        }

        [WebMethod]
        public DataSet DistributionMeterNotes_SelectAllIdName(string connectionKey)
        {
            return PPI.ServiceLogic.Distribution.LMeterNote.SelectAllIdName(connectionKey);
        }

        #endregion

        #region Inspections

        #region Inspection
        
        //[WebMethod]
        //public Int32 Inspections_Save(Int32 inspectionId, double latitude, double longitude, Int32 year, DateTime lVisit, string number, DateTime acquisitionDate, Int32 treatmentFk, Int32 classFk, Int32 SpecieFk, Int32 ToptypeFk, Int32 length, Int32 inspectorFk, Int32 companyFk, Int32 mapFk, Int32 projectFk, Int32 contactFk, Int32 assetFk, Int32 sectionFk, Int32 guardsQuantity, string lastCompanyName, string connectionKey)
        //{
        //    return
        //        LInspection.Save(inspectionId, latitude, longitude, year, lVisit, number, acquisitionDate, treatmentFk,
        //                         classFk, SpecieFk, ToptypeFk, length, inspectorFk, companyFk, mapFk, projectFk, contactFk, assetFk, sectionFk, guardsQuantity, lastCompanyName, connectionKey);
        //}

        [WebMethod]
        public Int32 Inspections_Save(Int32 inspectionId, double latitude, double longitude, Int32 year, DateTime lVisit, string number, DateTime acquisitionDate, Int32 treatmentFk, Int32 classFk, Int32 SpecieFk, Int32 ToptypeFk, Int32 length, Int32 inspectorFk, Int32 companyFk, Int32 mapFk, Int32 projectFk, Int32 contactFk, Int32 assetFk, Int32 sectionFk, Int32 guardsQuantity, bool poleTag, bool billable, bool groundWireRepair, string lastCompanyName, int visitTypeFk, string connectionKey)
        {
            return
                LInspection.Save(inspectionId, latitude, longitude, year, lVisit, number, acquisitionDate, treatmentFk,
                                 classFk, SpecieFk, ToptypeFk, length, inspectorFk, companyFk, mapFk, projectFk, contactFk, assetFk, sectionFk, guardsQuantity, poleTag, billable, groundWireRepair, lastCompanyName, visitTypeFk, connectionKey);
        }

        [WebMethod]
        public DataSet Inspections_GetByInspectionResult(Int32 inspectionId, string connectionKey)
        {
            return LInspection.GetInspectionById(inspectionId, connectionKey);
        }

        [WebMethod]
        public void Inspections_SetVisit(Int32 inspectionId, Int32 visitId, string connectionKey)
        {
            LInspection.SetVisit(inspectionId,visitId,connectionKey);
        }

        [WebMethod]
        public Int32 Inspections_NewVisit(Int32 inspectionId,List<Int32> inspectionList , string connectionKey)
        {
            return LInspection.NewVisitByPole(inspectionId, inspectionList, connectionKey);
        }

        [WebMethod]
        public Int32 Inspections_CreateRemedy(Int32 inspectionPoleFk, DateTime discoveredDate, Int32 inspectionResultTypeFk, string connectionKey)
        {
            return LInspection.InsertRemedy(inspectionPoleFk, discoveredDate, inspectionResultTypeFk, connectionKey);
        }

        [WebMethod]
        public void Inspections_UpdateRemedy(Int32 remedyId, Int32 remedyStatusFk, string remedyAssigned, DateTime remedyLastAction, DateTime remedyResolvedDate, string remedyNotes, string connectionKey)
        {
            LInspection.UpdateRemedy(remedyId,remedyStatusFk,remedyAssigned,remedyLastAction,remedyResolvedDate,remedyNotes,connectionKey);
        }

        #endregion

        #region InspectionResult
        [WebMethod]
        public Int32 InspectionResult_Save(Int32 inspectionResultId, Int32 InspectionFk, Int32 typeFk, Int32 resultTypeFk, string lastInspection, string comments,bool isElectrical, string connectionKey)
        {
            return
                LInspectionResult.Save(inspectionResultId, InspectionFk, typeFk, resultTypeFk, lastInspection, comments, connectionKey, isElectrical);
        }

        [WebMethod]
        public void InspectionResult_Delete(Int32 inspectionResultId, string connectionKey)
        {
            LInspectionResult.Delete(inspectionResultId,connectionKey);

        }

        [WebMethod]
        public DataSet InspectionResult_GetByInspectionResult(Int32 inspectionResultId, string connectionKey)
        {
            return LInspectionResult.GetInspectionResult(inspectionResultId, connectionKey);

        }

        [WebMethod]
        public DataSet InspectionResult_GetByInspectionResultByInspection(Int32 inspection, string connectionKey)
        {
            return LInspectionResult.GetInspectionResultByInspection(inspection, connectionKey);

        }

        #endregion

        #region InspectionImages

        [WebMethod]
        public Int32 InspectionImages_Save(Int32 imageId,Int32 InspectionFk, Int32 InspectionResultFk, string filename, string connectionKey)
        {
            return LInspectionImage.Save(imageId, InspectionFk, InspectionResultFk, filename, connectionKey);

        }

        [WebMethod]
        public void InspectionImages_Delete(Int32 imageId, string FileName, string connectionKey)
        {
            LInspectionImage.Delete(imageId,FileName,connectionKey);
        }

        [WebMethod]
        public DataSet InspectionImages_GetByInspection(Int32 inspectionId, string connectionKey)
        {
            return LInspectionImage.GetImagesByInspection(inspectionId, connectionKey);
        }

        #endregion

        #region InspectionCompany

        [WebMethod]
        public DataSet InspectionCompany_SelectAll(string connectionKey)
        {
            return LCompany.SelectAll(connectionKey);
        }

        #endregion

        #region InspectionInspector

        [WebMethod]
        public DataSet InspectionInspector_SelectAll(string connectionKey)
        {
            return LInspector.SelectAll(connectionKey);
        }

        #endregion

        #region InspectionMap

        [WebMethod]
        public DataSet InspectionMap_SelectAll(string connectionKey)
        {
            return LMap.SelectAll(connectionKey);

        }

        #endregion

        #region InspectionNotes

        [WebMethod]
        public DataSet InspectionNotes_SelectAllIdName(string connectionKey)
        {
            return LNote.SelectAllIdName(connectionKey);
        }
        [WebMethod]
        public DataSet InspectionNotes_GetNotesByInspectionResult(Int32 inspectionResultId, string connectionKey)
        {
            return LNote.GetNotesByInspectionResult(inspectionResultId, connectionKey);
        }

        [WebMethod]
        public Int32 InspectionNotes_Save(Int32 idNote, Int32 resultId,string connectionKey)
        {
            return LNote.Insert(idNote, resultId, connectionKey);
        }
        [WebMethod]
        public void InspectionNotes_DeleteByInspectionResult(Int32 resultId, string connectionKey)
        {
            LNote.DeleteByInspectionResult(resultId,connectionKey);
        }

        #endregion

        #region InspectionType
        [WebMethod]
        public DataSet InspectionType_SelectAllIdName(string connectionKey)
        {
            return LInspectionType.SelectAllIdName(connectionKey);
        }

        #endregion

        #region InspectionResultType

        [WebMethod]
        public DataSet InspectionResultType_SelectAllIdName(string connectionKey)
        {
            return LInspectionResultType.SelectAllIdName(connectionKey);
        }

        #endregion

        #region InspectionStructure

        [WebMethod]
        public void InspectionStructure_Delete(Int32 structureId,string connectionKey)
        {
            LInspectionStructure.Delete(structureId,connectionKey);
        }

        [WebMethod]
        public void InspectionStructure_DeleteWithPoles(Int32 structureId, string connectionKey)
        {
            LInspectionStructure.DeleteWithPoles(structureId, connectionKey);
        }

        #endregion



        #endregion

        #region ToSdf

        [WebMethod]
        public string GetDatabaseFile()
        {
            string fileName = Server.MapPath("") + "/BackupMobile/" + DateTime.Now.Ticks + ".sdf";
            //LDatabaseBackup _databaseBackup = new LDatabaseBackup();
            return fileName;

        }

        #endregion

        #region DSettings
        [WebMethod]
        public DataSet DSettings_SelectKml(string connectionKey)
        {
            return DSettings.SelectKml(connectionKey);
        }
        [WebMethod]
        public void DSettings_Update(int id, string text, string connectionKey)
        {
            DSettings.Update(id, text, connectionKey);
        }


        #endregion

        #region DVendor_Visit_XMissionStructure
        [WebMethod]
        public DataSet DVendor_Visit_XMissionStructure_GetServerVisits(string connectionKey)
        {
            return DVendor_Visit_XMissionStructure.GetServerVisits(connectionKey);
        }

        [WebMethod]
        public DataSet DVendor_Visit_XMissionStructure_GetServerVisitsById(Int32 id,string connectionKey)
        {
            return LVendor_Visit_XMissionStructure.GetVisitById(id, connectionKey);
        }
        
        [WebMethod]
        public DataSet DVendor_Visit_XMissionStructure_GetServerVisitsByUtilityID(int utilityID, string connectionKey)
        {
            return DVendor_Visit_XMissionStructure_GetServerVisitsByUtilityID(utilityID, connectionKey);
        }
        
        [WebMethod]
        public Int32 DVendor_Visit_XMissionStructure_Save(Int32 id, Int32 projectId, Int32 utilityId, Int32 visitTypeId, Int32 topTypeId, double latitude, double longitude, double altitude, Int32 satelliteCount, DateTime satelliteTime, bool solutionAvailable, Int32 handheldId, Int32 contactId, DateTime date, int totalImages, string connectionKey)
        {
            return LVendor_Visit_XMissionStructure.Save(id, projectId, utilityId, visitTypeId, topTypeId, latitude, longitude,
                                                 altitude, satelliteCount, satelliteTime, solutionAvailable, handheldId,
                                                 contactId, date, totalImages, connectionKey);
        }
        
        [WebMethod]
        public void DVendor_Visit_XMissionStructure_Delete(Int32 id,string connectionKey)
        {
            LVendor_Visit_XMissionStructure.Delete(id,connectionKey);
        }

        #endregion

        #region DVendor_Visit_XMissionStructures_Pole

        [WebMethod]
        public Int32 DVendor_Visit_XMissionStructures_Pole_Save(Int32 poleId, Int32 assetId, Int32 topTypeId, Int32 speciesId, Int32 treatmentId, Int32 year, Int32 classId, double latitude, double longitude, double altitude, Int32 satelliteCount, DateTime satelliteTime, string aid, Int32 lvisit, string number, double length, string lvisitor, bool solutionAvailable, string connectionKey)
        {
            return
                LVendor_Visit_XMissionStructures_Pole.Save(poleId, assetId, topTypeId, speciesId, treatmentId, year,
                                                           classId, latitude, longitude, altitude,satelliteCount,
                                                           satelliteTime, aid, lvisit, number, length, lvisitor,
                                                           solutionAvailable, connectionKey);
        }
        [WebMethod]
        public void DVendor_Visit_XMissionStructures_Pole_Delete(Int32 poleId, string connectionKey)
        {
            LVendor_Visit_XMissionStructures_Pole.Delete(poleId,connectionKey);
        }

        #endregion

        #region DVendor_Visit_XMissionStructures_Image
        [WebMethod]
        public DataSet DVendor_Visit_XMissionStructures_Image_GetServerImages(int offset, string connectionKey)
        {
            return DVendor_Visit_XMissionStructures_Image.GetServerImages(offset, connectionKey);
        }
        [WebMethod]
        public DataSet DVendor_Visit_XMissionStructures_Image_GetByStructureID(int structureID, string connectionKey)
        {
            return DVendor_Visit_XMissionStructures_Image.GetByStructureID(structureID, connectionKey);
        }
        [WebMethod]
        public Int32 DVendor_Visit_XMissionStructures_Image_Save(int imageId, int vendor_Visit_XMissionStructureFK, bool solutionAvaible, string fileName, double latitude, double altitude, double longitude, DateTime satelliteTime, int satelliteCount, double direction, int vendor_Visit_XMissionStructures_PoleID, string connectionKey)
        {
            return
                LVendor_Visit_XMissionStructures_Image.Save(imageId, vendor_Visit_XMissionStructureFK, solutionAvaible,
                                                            fileName, latitude, altitude, longitude, satelliteTime,
                                                            satelliteCount, direction,
                                                            vendor_Visit_XMissionStructures_PoleID, connectionKey);

        }

        [WebMethod]
        public void DVendor_Visit_XMissionStructures_Image_Delete(Int32 imageId, string fileName, string connectionKey)
        {
            LVendor_Visit_XMissionStructures_Image.Delete(imageId,connectionKey);
        }

        #endregion

        #region DCheckDatabaseConnection
        [WebMethod]
        public bool DCheckDatabaseConnection_CheckConnection(string connectionKey)
        {
            return DCheckDatabaseConnection.CheckConnection(connectionKey);
        }

        #endregion

        #region Utility

        [WebMethod]
        public DataSet DUtility_SelectAllIdName(string connectionKey)
        {
            return DUtility.SelectAllIdName(connectionKey);
        }

        [WebMethod]
        public Int32 DUtility_Asset_XMissionStructures_DuplicateByInspection(Int32 inspectionId,string connectionKey)
        {
            return LUtility_Asset_XMissionStructure.DuplicateByInspection(inspectionId, connectionKey);
        }

        [WebMethod]
        public Int32 DUtility_Asset_XMissionStructures_DuplicateByVisit(Int32 visitId, string connectionKey)
        {
            return LUtility_Asset_XMissionStructure.DuplicateByVisit(visitId, connectionKey);
        }

        [WebMethod]
        public Int32 DUtility_Asset_XMissionStructures_AssociateWithVisit(Int32 visitId, Int32 utilityId, string connectionKey)
        {
            return LUtility_Asset_XMissionStructure.AssociateWithVisit(visitId,utilityId, connectionKey);
        }

        [WebMethod]
        public Int32 DUtility_Asset_XMissionStructures_AssociateWithInspection(Int32 inspectionId, Int32 utilityId, string connectionKey)
        {
            return LUtility_Asset_XMissionStructure.AssociateWithInspection(inspectionId, utilityId, connectionKey);
        }

        [WebMethod]
        public DataSet DUtility_Asset_XMissionStructures_SelectAllNearByLatitudeLongitude(double latitude, double longitude,string connectionKey)
        {
            return LUtility_Asset_XMissionStructure.SelectAllNearByLatitudeLongitude(latitude, longitude, connectionKey);
        }

        [WebMethod]
        public void DUtility_Asset_XMissionStructures_AutolinkInspection(double maxdiatanceInMts,string connectionKey)
        {
            LUtility_Asset_XMissionStructure.AutolinkInspection(maxdiatanceInMts,connectionKey);
        }

        [WebMethod]
        public void DUtility_Asset_XMissionStructures_AutolinkVendor(double maxdiatanceInMts, string connectionKey)
        {
            LUtility_Asset_XMissionStructure.AutolinkVendor(maxdiatanceInMts, connectionKey);
        }

        #endregion

        #region Vendor

        [WebMethod]
        public DataSet DVendor_SelectData(Int32 VendorID, string connectionKey)
        {
            return DVendor.SelectData(VendorID, connectionKey);
        }

        [WebMethod]
        public DataSet DVendor_SelectAllIdName(string connectionKey)
        {
            return DVendor.SelectAllIdName(connectionKey);
        }

        #endregion

        #region Vendor_Contacts

        [WebMethod]
        public DataSet DVendor_Contacts_SelectAllIdNameByVendor(int vendorID, string connectionKey)
        {
            return DVendor_Contacts.SelectAllIdNameByVendor(vendorID, connectionKey);
        }

        #endregion

        #region Vendor_Project

        [WebMethod]
        public DataSet DVendor_Project_SelectData(Int32 Vendor_ProjectID, string connectionKey)
        {
            return DVendor_Project.SelectData(Vendor_ProjectID, connectionKey);
        }

        [WebMethod]
        public DataSet DVendor_Project_SelectAllIdName(string connectionKey)
        {
            return DVendor_Project.SelectAllIdName(connectionKey);
        }

        [WebMethod]
        public DataSet DVendor_Project_SelectAllIdNameByVendor(int vendorID, string connectionKey)
        {
            return DVendor_Project.SelectAllIdNameByVendor(vendorID, connectionKey);
        }

        [WebMethod]
        public DataSet DVendor_Project_SelectAll(string connectionKey)
        {
            return DVendor_Project.SelectAll(connectionKey);
        }

        #endregion

        #region Handheld

        [WebMethod]
        public DataSet DHandheld_SelectAllIdName(string connectionKey)
        {
            return DHandheld.SelectAllIdName(connectionKey);
        }

        [WebMethod]
        public int DHandheld_Insert(string code, string connectionKey)
        {
            return DHandheld.Insert(code, connectionKey);
        }

        #endregion

        #region Treatment

        [WebMethod]
        public DataSet Treatment_SelectAllIdName(string connectionKey)
        {
            return LXMissionStructures_PoleTreatments.SelectAllIdName(connectionKey);
        }

        #endregion

        #region Class

        [WebMethod]
        public DataSet Class_SelectAllIdName(string connectionKey)
        {
            return LXMissionStructures_PoleClass.SelectAllIdName(connectionKey);
        }

        #endregion

        #region Specie

        [WebMethod]
        public DataSet Specie_SelectAllIdName(string connectionKey)
        {
            return LXMissionStructures_PoleSpecies.SelectAllIdName(connectionKey);
        }

        #endregion

        #region VisitType

        [WebMethod]
        public DataSet VisitType_SelectAllIdName(string connectionKey)
        {
            return LVendor_Visit_Type.SelectAllIdName(connectionKey);
        }



        #endregion

        #region TopType

        [WebMethod]
        public DataSet TopType_SelectAllIdName(string connectionKey)
        {
            return LXMissionStructures_TopType.SelectAllIdName(connectionKey);
        }


        [WebMethod]
        public DataSet TopType_SelectAllIdNameByType(Int32 type,string connectionKey)
        {
            return LXMissionStructures_TopType.SelectAllIdNameByType(type,connectionKey);
        }

        #endregion

        #region Polygons

        [WebMethod]
        public DataSet Polygons_GetPolygons(string connectionKey)
        {
            return LPolygon.GetPolygons(connectionKey);
        }

        #endregion

        #region Users

        [WebMethod]
        public DataSet Users_GetAll(string connectionKey)
        {
            return LUser.GetAll(connectionKey);
        }

        [WebMethod]
        public Int32 Users_Save(Int32 userId, string username, string password, bool isAdministrator, string connectionKey)
        {
            return LUser.Save(userId, username, password, isAdministrator, connectionKey);
        }


        #endregion

        #region Settings
        [WebMethod]
        public DataSet Settings_GetByKey(string key, string connectionKey)
        {
            return DSettings.SelectSettingByKey(key, connectionKey);
        }
        [WebMethod]
        public void Settings_UpdateByKey(string key,string text,string value, string connectionKey)
        {
            DSettings.UpdateSettingByKey(key,text,value,connectionKey);
        }

        #endregion

        #region Remedy
        [WebMethod]
        public DataSet Remedy_SelectAll(string connectionKey)
        {
            return LRemedy.SeletAll(connectionKey);
        }

        [WebMethod]
        public DataSet RemedyStatus_GetAll(string connectionKey)
        {
            return LRemedyStatus.SelectAll(connectionKey);
        }

        #endregion

        #region CentralOffice

        [WebMethod]
        public DataSet CentralOffice_SelectAll(string connectionKey)
        {
            return LCentralOffice.SelectAll(connectionKey);
        }

        #endregion


    }
}
